package 题目集.二分;

public class 切绳子 {
    int[] arr;
    int k;
    public int maxLen(int[] arr,int k){
        this.arr=arr;
        this.k=k;
        int l=1,r=getMax();

        while (l<=r){
            int m=(l+r)/2;
            if (check(m)) {
                l = m + 1;
            }else {
                r = m - 1;
            }
        }

        if (check(l)) return l;
        return r;
    }
    public boolean check(int m){
        int cnt=0;
        for (int i = 0; i < arr.length; i++) {
            cnt+=arr[i]/m;
        }
        return cnt>=k;
    }
    public int getMax(){
        int max=0;
        for (int i = 0; i < arr.length; i++) {
            max=Math.max(max,arr[i]);
        }
        return max;
    }
}
